﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

namespace JustPlus
{
    /// <summary>
    /// UpdatePanel.xaml 的交互逻辑
    /// </summary>
    public partial class UpdatePanel : UserControl
    {
        //更新过程中不准退出该窗口
        public static string currentversion = @"v1.1";
        private string releasedate = @"2012/3/5";
        //public static bool HasUpdate = false;
        private string currentfilepath = System.Windows.Forms.Application.StartupPath;
        public UpdatePanel()
        {
            InitializeComponent();
            this.Loaded+=new RoutedEventHandler(UpdatePanel_Loaded);
        }

        public static bool CheckUpdate()
        {
            bool HasUpdate = false;
            string s = new ConnectionBase().Get(@"http://www.win7soft.com/doucontorl/justlisten/update.htm");
            if (string.IsNullOrEmpty(s))
                HasUpdate = false;
            else
            {
                Match match = Regex.Match(s, @"<td>Version:([^\""]+)</td>");
                string version = match.Groups[1].Value;
                if (version == currentversion)
                    HasUpdate = false;
                else
                    HasUpdate = true;
            }
            return HasUpdate;
        }

        private void UpdatePanel_Loaded(object sender, EventArgs e)
        {
            listBox1.Items.Add("正在检查更新...");
            listBox1.Items.Add("当前版本：" + currentversion + "   发布时间：" + releasedate);
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (CheckUpdate())
                        {
                            listBox1.Items.Add("软件有更新，请选择更新方式进行更新！");
                            button1.IsEnabled = true;
                            button2.IsEnabled = true;
                        }
                        else
                        {
                            listBox1.Items.Add("当前版本已是最新！不需要更新！");
                            button1.IsEnabled = false;
                            button2.IsEnabled = false;
                        }
                    }));
            }));
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            if (mwnd != null)
            {
                mwnd.stackPanel1.Children.Clear();
                mwnd.stackPanel1.Children.Add(mwnd.stackPanel2);
                mwnd.stackPanel1.Children.Add(mwnd.stackPanel3);
            }
        }

        //自动更新
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(@"UpdateClient.exe");
            Application.Current.Shutdown();
        }

        //手动更新
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.win7soft.com/doucontorl/?post=540");
        }
    }
}